package net.tngou.iask.service;

import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.dao.PatientDao;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Address;
import net.tngou.iask.pojo.Linkman;
import net.tngou.iask.pojo.Main;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.pojo.Patient_main;
import net.tngou.iask.pojo.Patient_user;
import net.tngou.iask.pojo.User;
import net.tngou.iask.util.StringUtil;
import net.tngou.iask.util.ValidateTools;
import net.tngou.iask.util.IDUtil.No;

/**
 * 患者
 * @author tngou@tngou.net
 *
 */
public class PatientService extends BaseService {
	/**
	 * 取得患者
	 * @param page 当前页
	 * @param size 页大小
	 * @param main 实体
	 * @param keyword 关键词
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,String keyword) {
		PatientDao dao = new PatientDao();
		return dao.getPage(page, size, main, keyword);
	}
	public PageUtil getPageUser(int page,int size,long user,String keyword) {
		PatientDao dao = new PatientDao();
		return dao.getPageUser(page, size, user, keyword);
	}
	
	/**
	 * 添加病人
	 * @param bean
	 * @param no
	 * @param user
	 * @param phone
	 * @param relation 
	 * @return
	 */
	public long save(Patient bean,No no,User user,String phone ,int relation) {
		if(bean.getBirthday()==null)bean.setBirthday(no.getBirthday());
		if(bean.getGender()==0)bean.setGender(no.getGender());
		Address address=null;
		if(bean.getBirthcity()==0)
		{
			address = new Address().get(Long.parseLong(no.getLand()));
			if(address==null){
				address= new Address();
				address.setId(1);
				address.setName("中国");
			}
			
			bean.setBirthcity(address.getId());
			if(StringUtil.isEmpty(bean.getBirthaddress()))
			{
					int type=0;
					String adds="";
					do {						
						adds=address.getName()+" "+adds;
						address = address.get(address.getParent_id());
						if(address!=null)						
						type=address.getType();
						else type=0;
					} while (type>1);
					bean.setBirthaddress(adds);//设置出生地
			}
			
		}
		Main main=null;
		Address laddress = null;
		if(user.getMain()>0)main=new Main().get(user.getMain());
		if(main!=null)laddress =new  Address().get(main.getProvince());
		if(laddress==null)
		{
			laddress=address;
			if(laddress==null)
			{
				laddress= new Address();
				laddress.setId(1);
				laddress.setName("中国");
			}
		}
		bean.setLowcity(laddress.getId());
		bean.setLowaddress(laddress.getName());
		bean.setNation("汉族");
		bean.setAlive(1);//
		long id = bean.save();
		if(id>0)
		{
			if(user.getRole()>0&&ValidateTools.Mobile(phone))
			{
				saveLinkmain(bean, phone, relation, 1);
			}
			if(user.getRole()>0&&user.getMain()>0)//系统管理
			{
				 Patient_main pm = new  Patient_main().get("patient="+id+" AND main="+user.getMain());
				 if(pm==null)
				 {
					 pm= new Patient_main();
					 pm.setMain(user.getMain());
					 pm.setPatient(id);
					 pm.save();
	
				 }
				 
			}else if(user.getRole()==0)//普通用户
			{
				Patient_user pu=new Patient_user().get("patient="+id+" AND user="+user.getId());
				if(pu==null)
				{
					pu=new Patient_user();
					pu.setPatient(id);
					pu.setUser(user.getId());
					pu.setRelation(0);
					if(relation!=0)
						pu.setRelation(relation);
					pu.save();

				}
				Linkman linkman = new Linkman();
				linkman.setName(bean.getName());
				linkman.setTel(user.getAccount());
				linkman.setRelation(pu.getRelation());
				linkman.setIsuse(1);
				linkman.setPatient(id);
				linkman.setIsdefault(1);
				linkman.save();
				
			}
		}
		return id;
				
	}
	/**
	 * 确定实体下的所有用户
	 * @param main
	 * @return
	 */
	public List<Patient> list(long main,int type)
	{
		PatientDao dao =new PatientDao();
		return dao.list(main,type);			
	}
	
	
	public void saveLinkmain(Patient bean,String phone,int relation,int isdefault) {
		
		if(ValidateTools.Mobile(phone))
		{ 
			 POJO pojo = new  Linkman().get("patient="+bean.getId()+" AND tel="+phone);
			 if(pojo!=null) return;
			 Linkman linkman = new Linkman();
			 linkman.setName(bean.getName());
			 linkman.setTel(phone);
			 linkman.setPatient(bean.getId());
			 linkman.setRelation(relation);
			 linkman.setIsuse(1);
			 linkman.setIsdefault(1);
			 linkman.save();
		}
	}
}
